package com.zhiche.lisa.lspm.service.statistic.impl;

import com.google.common.collect.Lists;
import com.zhiche.lisa.lspm.dao.mapper.statistics.StatisticsMapper;
import com.zhiche.lisa.lspm.dao.model.statistics.bo.StatisticBO;
import com.zhiche.lisa.lspm.service.statistic.StatisticService;
import com.zhiche.lisa.lspm.vo.statistic.StatisticVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public List<StatisticBO> getStatics(String startDate) {
        List<StatisticBO> list = statisticsMapper.getStaticsNoTenant(startDate);
        return list;
    }

    @Override
    public List<StatisticVO> getListStatics(String startDate) {
        List<StatisticBO> list = this.getStatics(startDate);
        List<StatisticVO> statisticVOList = Lists.newArrayList();

        //中联实际承运商数量
        StatisticVO statisticVOD4 = getStatisticVOByItemCode(list,"D4");
        statisticVOList.add(statisticVOD4);

        //已注册有效的承运商数
        StatisticVO statisticVOD5 = getStatisticVOByItemCode(list,"D5");
        statisticVOList.add(statisticVOD5);

        //注册率=承运商注册数/实际承运商总数
        StatisticVO statisticVOD6 = getDivisionByList(list,"D5","D4");
        statisticVOList.add(statisticVOD6);

        //已认证的承运商数
        StatisticVO statisticVOD7 = getStatisticVOByItemCode(list,"D7");
        statisticVOList.add(statisticVOD7);

        //认证率=承运商认证数/承运商注册数
        StatisticVO statisticVOD8 = getDivisionByList(list,"D7","D5");
        statisticVOList.add(statisticVOD8);

        //已注册有效的司机数
        StatisticVO statisticVOD9 = getStatisticVOByItemCode(list,"D9");
        statisticVOList.add(statisticVOD9);

        //已认证司机数
        StatisticVO statisticVOD10 = getStatisticVOByItemCode(list,"D10");
        statisticVOList.add(statisticVOD10);

        //认证率=司机认证数/司机注册数
        StatisticVO statisticVOD11 = getDivisionByList(list,"D10","D9");
        statisticVOList.add(statisticVOD11);

        //已注册有效的牵引车数
        StatisticVO statisticVOD12 = getStatisticVOByItemCode(list,"D12");
        statisticVOList.add(statisticVOD12);

        //已认证牵引车数
        StatisticVO statisticVOD13 = getStatisticVOByItemCode(list,"D13");
        statisticVOList.add(statisticVOD13);

        //认证率=牵引车认证数/牵引车注册数
        StatisticVO statisticVOD14 = getDivisionByList(list,"D13","D12");
        statisticVOList.add(statisticVOD14);

        //已注册有效的挂车数
        StatisticVO statisticVOD15 = getStatisticVOByItemCode(list,"D15");
        statisticVOList.add(statisticVOD15);

        //已认证挂车数
        StatisticVO statisticVOD16 = getStatisticVOByItemCode(list,"D16");
        statisticVOList.add(statisticVOD16);

        //认证率=挂车认证数/挂车注册数
        StatisticVO statisticVOD17 = getDivisionByList(list,"D16","D15");
        statisticVOList.add(statisticVOD17);

        //当天调度指令生成数
        StatisticVO statisticVOD18 = getStatisticVOByItemCode(list,"D18");
        statisticVOList.add(statisticVOD18);

        //当天报班生成数
        StatisticVO statisticVOD19 = getStatisticVOByItemCode(list,"D19");
        statisticVOList.add(statisticVOD19);

        //报班使用率=报班数/指令数
        StatisticVO statisticVOD20 = getDivisionByList(list,"D19","D18");
        statisticVOList.add(statisticVOD20);

        //当天在途位置上报数
        StatisticVO statisticVOD21 = getStatisticVOByItemCode(list,"D21");
        statisticVOList.add(statisticVOD21);

        //当天运抵确认数量
        StatisticVO statisticVOD22 = getStatisticVOByItemCode(list,"D22");
        statisticVOList.add(statisticVOD22);

        return statisticVOList;
    }


    /**
     * 根据项目编码得到计算类
     * @param list
     * @param itemCode
     * @return
     */
    private StatisticVO getStatisticVOByItemCode(List<StatisticBO> list,String itemCode){
        List<Integer> listD5 = list.stream().filter(statisticBO -> statisticBO.getItemCode().equals(itemCode)).sorted(Comparator.comparing(StatisticBO::getDate)).map(StatisticBO::getCount).collect(Collectors.toList());
        StatisticVO statisticVOD5 = new StatisticVO();
        List<String> listStrD5 = Lists.newArrayList();
        CollectionUtils.collect(listD5,x -> x.toString(),listStrD5);
        statisticVOD5.setDetailList(listStrD5);
        int sum = listD5.stream().mapToInt(Integer::intValue).sum();
        statisticVOD5.setTotal(String.valueOf(sum));
        return statisticVOD5;
    }

    /**
     * 根据列表得到求比例的方法
     * @param list
     * @param itemCode
     * @param itemCode2
     * @return
     */
    private StatisticVO getDivisionByList(List<StatisticBO> list,String itemCode,String itemCode2){
        DecimalFormat df=new DecimalFormat("0.00");
        //得到每个项目列表
        List<Integer> list1 = list.stream().filter(statisticBO -> statisticBO.getItemCode().equals(itemCode)).sorted(Comparator.comparing(StatisticBO::getDate)).map(StatisticBO::getCount).collect(Collectors.toList());
        List<Integer> list2 = list.stream().filter(statisticBO -> statisticBO.getItemCode().equals(itemCode2)).sorted(Comparator.comparing(StatisticBO::getDate)).map(StatisticBO::getCount).collect(Collectors.toList());

        //得到每日的比例
        List<String> list3 = Lists.newArrayList();
        for (int i = 0; i < list1.size(); i++){
            Integer int1 = list1.get(i);
            Integer int2 = list2.get(i);

            if(int2.equals(0)){
                list3.add("0.00%");
            }
            else{
                float divsion = (float)int1*100/int2;
                String strRtn = df.format(divsion) + "%";
                list3.add(strRtn);
            }
        }

        StatisticVO statisticVO = new StatisticVO();
        statisticVO.setDetailList(list3);
        int intSum1 = 0;
        int intSum2 = 0;

        for (int i = 0; i < list1.size(); i++) {
            intSum1 = list1.get(i) + intSum1;
        }

        for (int i = 0; i < list2.size(); i++) {
            intSum2 = list2.get(i) + intSum2;
        }
        float avg = 0f;
        if(intSum2 != 0){
            System.out.print(intSum1);
            System.out.print(intSum2);
            avg = (float)intSum1*100/(float)intSum2;
        }
        statisticVO.setTotal(df.format(avg) + "%");
        return statisticVO;
    }
}
